package com.chaoyue.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.chaoyue.entity.Message;

public class sendServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String msg = request.getParameter("msg");
		
		HttpSession session = request.getSession();
		String name = (String) session.getAttribute("user");
		
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		String time =sdf.format(new Date());
		
		Message m = new Message();
		m.setMsg(msg);
		m.setName(name);
		m.setTime(time);
		
		ServletContext application = getServletContext();
		List<Message> messageList = (List<Message>) application.getAttribute("messageList");
		
		if(messageList==null){
			messageList = new ArrayList<Message>();
			
		}
		messageList.add(m);
		application.setAttribute("messageList", messageList);
		response.sendRedirect("send.jsp");
	}

}
